﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LigerMVC.Common;
using LigerMVC.Entities;
using LigerMVC.Business.ICore;
using LigerMVC.IDataAccess;

namespace LigerMVC.Business.Core
{
    public class UserBusiness  : IUserBusiness
    {
        private readonly IDataAccess<CF_User> _userDA;

        public UserBusiness(IDataAccess<CF_User> userDA)
        {
            _userDA = userDA; 
        }
        public CF_User GetUser(int id)
        {
            return _userDA.Find(a => a.UserID == id);
        }


        public PagedData<CF_User> GetPagedUser(QueryDescriptor descriptor)
        {
            return _userDA.GetPaged(descriptor);
        }

        public bool SaveUser(CF_User user)
        {
            if (user == null)
            {
                return false;
            }
            user.ModifyDate = DateTime.Now;
            user.ModifyUserID = SysContext.CurrentUserID;

            if (user.UserID == 0)
            {
                user.CreateDate = DateTime.Now;
                user.CreateUserID = SysContext.CurrentUserID;
                user.RecordStatus = Common.RecordStatus.Active;
                user.Guid = Guid.NewGuid().ToStr();
                return _userDA.Insert(user);
            }
            return _userDA.Update(user);
        }

        public void DeleteUser(CF_User user)
        {
            _userDA.Delete(user); 
        }
    }
}
